package com.wcity.manage.web.block;

import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.JSONUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.service.BlockService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by jn on 2016/12/12.
 * 小区信息接口
 */
@Controller
public class IBlockAct {

    /**
     * 获取小区列表
     * @param area
     * @param blockName
     * @return
     */
    @RequestMapping("block_list_front")
    @ResponseBody
    public Map<String,Object> blockListFront(String area, String blockName, String registerId){

        if ( StringUtil.isBlank( area ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object o = blockService.findBlockList( area, blockName, registerId );
        if ( o instanceof String ) throw new CustomException( (String)o, Code.ERROR );

        Map<String,Object> map = JSONUtil.getSuccessJson();
        map.put("blockList",o);
        return map;
    }

    /**
     * 入住小区  返回物业社区id 用于查询模块信息
     * @param registerId
     * @param blockId
     * @return
     */
    @RequestMapping("block_register_in")
    @ResponseBody
    public Object blockRegisterIn(String registerId,String blockId,String token){

        if ( !StringUtil.isNumeric( blockId ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        if ( !StringUtil.isBlank( registerId ) && !StringUtil.isNumeric( registerId ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        if ( !StringUtil.isBlank( token ) && token.length() > 50 )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object errorMsg = blockService.blockRegisterIn( blockId, registerId, token );

        if ( errorMsg instanceof String ) throw new CustomException( (String)errorMsg, Code.ERROR );

        return errorMsg;
    }

    /**
     * 小区banner图片
     * @param blockId
     * @return
     */
    @RequestMapping("block_image_list")
    @ResponseBody
    public Map<String,Object> blockImageList(String blockId){

        if ( !StringUtil.isNumeric( blockId ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object o = blockService.getBlockImageList( blockId );

        if ( o instanceof String ) throw new CustomException( (String) o, Code.ERROR );

        Map<String,Object> map = JSONUtil.getSuccessJson();
        map.put("imageList",o);
        return map;
    }

    @Resource private BlockService blockService;

}
